<?php
/**
 * @author Mingming Tang iamalazyrat@gmail.com
 */

class Image_Controller extends Base_Controller {

  public $restful = true;

  public function get_index() {
    return Redirect::to(URL::to('image/list'));
  }

  public function get_list() {
    $per_page = 40;
    $page = Input::get('p', 1);
    $tag = Input::get('tag', false);
    $collection_id = Input::get('collection_id', false);
    if (!$tag AND !$collection_id) {
      $imgs = Img::with('product')->with('tags')->order_by('updated_at', 'desc')->take($per_page)->skip(($page-1)*$per_page)->get();
      $cnt = Img::with('product')->with('tags')->order_by('updated_at', 'desc')->count();
    } else {
      $tags = Tag::with('imgs')->where('name', '=', $tag)->get();
      $img_id = array();
      if ($tags) {
        foreach ($tags as $tag) {
          foreach ($tag->imgs as $i) {
            $img_id[] = $i->id;
          }
	}
      }
      $collection = Collection::find($collection_id);
      $img_id2 = array();
      if ($collection) {
        foreach ($collection->imgs as $i) {
          $img_id2[] = $i->id;
        }
      }
      $img = Img::with('product')->with('tags')->order_by('updated_at', 'desc')->take($per_page)->skip(($page-1)*$per_page);
      if (!empty($img_id)) $img->where_in('id', $img_id);
      if (!empty($img_id2)) $img->where_in('id', $img_id2);
      $imgs = $img->get();
      $cnt = $img->count();
    }
    $tag = isset($tag->name) ? $tag->name : '';
    $url = url('image/list') . "?tag={$tag}&collection_id={$collection_id}";
    $page = Pagination::page($page, $cnt, 40, $url);
    return View::make('image.list.main')->with('imgs', $imgs)->with('page', $page)->with('collection_id', $collection_id);
  }

  function post_add_tag() {
    $data = Input::all();
    if (!isset($data['tags']) OR !isset($data['img_id'])) {
      return Response::json(array('code', '400'));
    }
    $tags = explode(',', $data['tags']);
    $img = Img::find($data['img_id']);
    foreach ($tags as $t) {
      if (trim($t) == '') continue;
      $tag = Tag::where('name', '=', $t)->where('collection_id', '=', $img->collection_id)->first();
      if (!$tag) $tag = Tag::create(array('name' => $t, 'collection_id'=>$img->collection_id));
      if (!DB::table('img_tag')->where('img_id', '=', $data['img_id'])->where('tag_id', '=', $tag->id)->first()) {
        DB::table('img_tag')->insert(array('img_id' => $data['img_id'], 'tag_id'=>$tag->id));
      }
    }
    return Response::json(array('code'=>200));
  }

  function post_edit_tag() {
    $data = Input::all();
    if (!isset($data['tags']) OR !isset($data['img_id'])) {
      return Response::json(array('code', '400'));
    }
    $tags = explode(',', $data['tags']);
    $img = Img::find($data['img_id']);
    if (!$img) return Response::json(array('code', 400));
    DB::table('img_tag')->where('img_id', '=', $data['img_id'])->delete();
    foreach ($tags as $t) {
      if (trim($t) == '') continue;
      $tag = Tag::where('name', '=', $t)->where('collection_id', '=', $img->collection_id)->first();
      if (!$tag) $tag = Tag::create(array('name' => $t, 'collection_id'=>$img->collection_id));
      if (!DB::table('img_tag')->where('img_id', '=', $data['img_id'])->where('tag_id', '=', $tag->id)->first()) {
        DB::table('img_tag')->insert(array('img_id' => $data['img_id'], 'tag_id'=>$tag->id));
      }
    }
    return Response::json(array('code'=>200));
  }

  public function post_delete() {
    $img_id = Input::get('id', false);
    $img = Img::find($img_id);
    if ($img) {
      $img->tags()->delete();
      $img->delete();
      return Response::json(array('code'=>200));
    } else {
      return Response::json(array('code'=>400));
    }


  }
}

